package cn.wolfcode.consumer._05_delay;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.List;

public class Consumer {
    public static void main(String[] args) throws Exception {
        //1.创建消费者对象
        DefaultMQPushConsumer defaultMQPushConsumer = new DefaultMQPushConsumer("consumer-demo");
        //2.指定名字服务器地址
        defaultMQPushConsumer.setNamesrvAddr("127.0.0.1:9876");
        //3.绑定消息的主题
        defaultMQPushConsumer.subscribe("05-delay","*");
        //4.监听处理消息的方法
        defaultMQPushConsumer.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
                for (MessageExt messageExt : list) {
                    System.out.println("消息的内容是:"+new String(messageExt.getBody()));
                    System.out.println("消息的id是:"+messageExt.getMsgId());
                }
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });
        //5.启动消费者
        defaultMQPushConsumer.start();
    }
}
